﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NewsCrawler.Common.Interfaces;
using System.Reflection;

namespace CrawlerManager
{
    public class CrawlerFactory
    {
        public static ICrawler CreateCrawler(Assembly assembly)
        {
            //Type type = assembly.GetTypes().Where((t) => t.GetInterfaces() == typeof(ICrawler)).FirstOrDefault();

            Type instanceType = null;
            foreach (Type type in assembly.GetTypes())
            {
                if(type.GetInterface("NewsCrawler.Common.Interfaces.ICrawler") != null)
                {
                    instanceType = type;
                    break;
                }
            }

            if(instanceType == null)
                throw new Exception();

            ICrawler createdObject = Activator.CreateInstance(instanceType) as ICrawler;
            return createdObject;
        }
    }
}
